package com.urea.system.service.impl;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.urea.common.core.domain.entity.Customer;
import com.urea.common.utils.SecurityUtils;
import com.urea.common.utils.StringUtils;
import com.urea.system.constants.BusinessConstants;
import com.urea.system.domain.vo.CustomerStatisticsVo;
import com.urea.system.domain.vo.CustomerVo;
import com.urea.system.domain.vo.FleetUserVo;
import com.urea.system.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.urea.system.mapper.CustomerMapper;

/**
 * 客户信息Service业务层处理
 *
 * @author liuzd
 * @date 2023-08-01
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private IFleetUserService fleetUserService;
    @Autowired
    private ITerminalService terminalService;
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IFleetService fleetService;

    /**
     * 查询客户信息
     *
     * @param id 客户信息主键
     * @return 客户信息
     */
    @Override
    public Customer selectCustomerById(Long id) {
        return customerMapper.selectCustomerById(id);
    }

    /**
     * 查询客户信息列表
     *
     * @param customer 客户信息
     * @return 客户信息
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer) {
        return customerMapper.selectCustomerList(customer);
    }

    /**
     * 新增客户信息
     *
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public int insertCustomer(Customer customer) {
        return customerMapper.insertCustomer(customer);
    }

    /**
     * 修改客户信息
     *
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public int updateCustomer(Customer customer) {
        return customerMapper.updateCustomer(customer);
    }

    /**
     * 批量删除客户信息
     *
     * @param ids 需要删除的客户信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerByIds(Long[] ids) {
        return customerMapper.deleteCustomerByIds(ids);
    }

    /**
     * 删除客户信息信息
     *
     * @param id 客户信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerById(Long id) {
        return customerMapper.deleteCustomerById(id);
    }

    /**
     * 根据手机号获取用户信息
     *
     * @param phone 手机号
     * @return 用户信息
     */
    @Override
    public CustomerVo getInfoByPhone(Long phone) {
        CustomerVo customerVo = customerMapper.getInfoByPhone(phone);
        FleetUserVo fleetInfo = fleetUserService.getFleetInfoByUid(customerVo.getId());
        customerVo.setFleetUserVo(fleetInfo);
        return customerVo;
    }


    @Override
    public CustomerVo queryStatisticsByDate(Long userId, Long agentId, Long storeId) {
        return customerMapper.queryStatisticsByDate(userId, agentId, storeId);
    }

    /**
     * pc端用户统计列表
     *
     * @param vo 查询信息
     * @return 结果
     */
    @Override
    public List<CustomerStatisticsVo> statisticsList(CustomerVo vo) {
        SecurityUtils.getUserIdByUserType(vo);
        return customerMapper.statisticsList(vo);
    }

    /**
     * 统计结果数据
     *
     * @param vo 条件
     * @return 结果
     */
    @Override
    public CustomerStatisticsVo statistics(CustomerVo vo) {
        SecurityUtils.getUserIdByUserType(vo);
        List<Map<String, String>> list = customerMapper.queryStatistics(vo);
        CustomerStatisticsVo result = new CustomerStatisticsVo();
        long vipCount = 0L;
        long ordinaryCount = 0L;
        if (CollectionUtils.isNotEmpty(list)) {
            for (Map<String, String> map : list) {
                // 0普通用户，1电子卡用户，2车队用户
                String userType = String.valueOf(map.get("userType"));
                String typeCount = String.valueOf(map.get("typeCount"));
                if (StringUtils.isNotBlank(typeCount)) {
                    if ("0".equals(userType)) {
                        ordinaryCount = Long.parseLong(typeCount);
                        result.setOrdinaryUserCount(Long.valueOf(typeCount));
                    }else {
                        vipCount += Long.parseLong(typeCount);
                    }
                }
            }
            result.setVipUserCount(vipCount);
            result.setAllUserCount(vipCount + ordinaryCount);
        }
        // 供应商数量
        result.setAgentCount(terminalService.selectAgentCountByCondition(vo));
        // 广告
        result.setBannerCount(bannerService.count());
        // 车队数
        if (BusinessConstants.ADMIN_TYPE.equals( SecurityUtils.getLoginUser().getUser().getUserType())){
            result.setFleetCount(fleetService.count());
        }else{
            result.setFleetCount(terminalService.selectFleetCountByCondition(vo));
        }
        // 车队用户数
        result.setFleetUserCount(terminalService.selectFleetUserCountByCondition(vo));
        return result;
    }

    /**
     * pc端用户管理
     *
     * @param vo 条件
     * @return 结果
     */
    @Override
    public List<CustomerVo> queryList(CustomerVo vo) {
        SecurityUtils.getUserIdByUserType(vo);
        return baseMapper.queryList(vo);
    }
}
